<template>
	<view class="container padding-top-sm">
		<view class="bg-white">
			<view class="solid-bottom text-13 flex justify-between align-center padding">
				<view class="text-333 flex-sub text-bold">
					收货人姓名
				</view>
				<input type="text" v-model="address.receiver" value="" class="text-13 flex-treble"
					placeholder="请填写收货人姓名" />
			</view>
			<view class="solid-bottom text-13 flex justify-between align-center padding">
				<view class="text-333 flex-sub text-bold">
					联系电话
				</view>
				<input v-model="address.mobile" type="number" value="" class="text-13 flex-treble"
					placeholder="请填写联系电话" />
			</view>
			<view class="solid-bottom text-13 flex justify-between align-center padding">
				<view class="text-333 text-bold">
					配送地址
				</view>
				<view class="flex justify-start align-center">
					<view class="text-767696 margin-right-xs" @click="changeAddress">
						{{address.addressCode || '请选择'}}
					</view>
					<image :src="baseUrl + '/static/right.png'" mode="" style="width: 22rpx;height: 22rpx;"></image>
				</view>
			</view>
			<view class="margin-bottom-sm text-13 flex justify-between align-center padding">
				<view class="text-333 flex-sub text-bold">
					详细地址
				</view>
				<input type="text" v-model="address.addressInfo" value="" class="text-13 flex-treble"
					placeholder="详细地址，例1号楼1层1001室" />
			</view>
		</view>
		<view class="bg-white margin-top-sm padding flex justify-between align-center text-13">
			<view class="text-333 flex-sub text-bold">
				设为默认地址
			</view>
			<view class="flex justify-start align-center flex-treble">
				<image v-if='!address.isdefault' @click="handChange(1)" :src="baseUrl + '/static/unselected.png'"
					mode=""></image>
				<image v-else @click="handChange(0)" :src="baseUrl + '/static/selected.png'" mode=""></image>
				<view class="margin-left-xs text-666">

					默认地址
				</view>
			</view>
		</view>
		<button class="bg-color text-15 text-white padding round margin-sm" @click="submit">保存</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				baseUrl: this.$config.baseUrl,
				address: {
					receiver: '',
					isdefault: 0,
					mobile: '',
					addressInfo: '',
					latitude: '',
					longitude: '',
					addressCode: '',
					userId: ''
				},


			};
		},
		onLoad(options) {
			if (options.id) {

				uni.setNavigationBarTitle({
					title: '地址编辑'
				})
				this.userId = options.id
				this.getDetail()
			}
		},
		methods: {
			changeAddress() {
				let that = this;
				uni.chooseLocation({
					success: function(res) {
						that.address.addressCode = res.address
						that.address.longitude = res.longitude;
						that.address.latitude = res.latitude;
						that.address.addressInfo = res.name;
					},
				});
			},
			handChange(index) {
				console.log(index)
				this.address.isdefault = index
			},
			async submit() {

				const {
					receiver,
					mobile,
					longitude,
					latitude,
					isdefault,
					addressInfo,
					addressCode
				} = this.address
				if (!receiver) {
					return this.$util.msg('收货人姓名不能为空')
				}
				if (!mobile) {
					return this.$util.msg('联系电话不能为空')
				}
				if (!addressCode) {
					return this.$util.msg('配送地址不能为空')
				}
				if (!addressInfo) {
					return this.$util.msg('详细地址不能为空')
				}
				const data = {
					receiver,
					mobile,
					longitude,
					latitude,
					isdefault,
					addressInfo,
					addressCode
				}

				// if(!receiver){
				// 	return this.$util.msg('收货人姓名不能为空')
				// }
				let api = ''
				if (this.userId) {
					api = 'addressUpdate'
					data['id'] = this.address.id
					data['userId']  = uni.getStorageSync('userId')
				} else {
					api = 'addressAdd'
				}

				const [err, res] = await this.$jsonPost(this.$api[api], data)
				if (res) {
					console.log(res)
					this.$util.msg(res.message)
					setTimeout(() => {
						uni.navigateBack({

						})
					}, 1000)
				}
			},
			async getDetail() {
				const data = {
					addrId: this.userId,
					userId: uni.getStorageSync('userId')
				}
				const [err, rs] = await this.$get(this.$api.addressDetail, data)
				if (rs) {
					this.address = rs.data
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	image {
		width: 26rpx;
		height: 26rpx;
	}
</style>
